package com.ruoyi.web.controller.initData;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.domain.dao.SysPostDao;
import com.ruoyi.system.domain.dao.SysUserDao;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Component
public class SysUserDutyAndSortNumInit {

    @Autowired
    private OaMapper oaMapper;
    @Autowired
    private SysUserDaoMapper sysUserDaoMapper;
    @Autowired
    private SysDeptDaoMapper sysDeptDaoMapper;
    @Autowired
    private SysPostDaoMapper sysPostDaoMapper;
    @Autowired
    private SysUserPostMapper sysUserPostMapper;

    /**
     *  初始化 用户岗位，绑定岗位到具体用户
     */
    public void bindPost2User() {
        List<SysUserDao> list = sysUserDaoMapper.selectList(new QueryWrapper<>());
        if (list != null && list.size() > 0) {
            list.stream().forEach(item -> {
                // userUuid=b96e1852522a46399b914fcd8e798d2c,empId=15d41a949a5e487ca6e9ff37f2f60f1e
                if (StringUtils.isEmpty(item.getOldData())) {
                    return;
                }
                String[] split = item.getOldData().split(",");
                if (split.length > 1) {
                    String userUuid = split[0].split("=")[1];
                    String empId = split[1].split("=")[1];
                    List<Map<String, String>> listData = oaMapper.listDataByCondition("ent_t_humen_root", "emp_id", empId);
                    if (listData != null && listData.size() > 0) {
                        Map<String, String> val = listData.get(0);
                        // {"dutys":[{"d_id":"9f057dbf32f14618b40083efa9090919"},{"d_id":"043b40ea393c4f0d967e88e90490e6a4"}]}
                        String dutyJson = val.get("duty_title"); // 旧数据所在部门

                        JSONObject jsonObj = JSONUtil.parseObj(dutyJson);
                        if (jsonObj!=null) {
                            jsonObj.getJSONArray("dutys").stream().forEach(item1 -> {
                                String dutyId = ((JSONObject)item1).getStr("d_id");
                                // dutyId=1000006,dutyUuid=6c3d6d0ccc8f4ff8a411eb1fd4079fab,depUuid=2c7b5f6cbec64845be545d57916db80a
                                LambdaQueryWrapper lw = new LambdaQueryWrapper<SysPostDao>()
                                        .like(SysPostDao::getOldData, String.format("dutyUuid=%s", dutyId));
                                SysPostDao sysPostDao = sysPostDaoMapper.selectOne(lw);
                                if (sysPostDao != null) {
                                    SysUserPost sysUserPost = new SysUserPost();
                                    sysUserPost.setUserId(item.getUserId());
                                    sysUserPost.setPostId(sysPostDao.getPostId());
                                    sysUserPostMapper.batchUserPost(Arrays.asList(sysUserPost));
                                }
                            });
                        }
                    }
                }
            });
        }
    }
}
